<?php
 
namespace Modules\ShopAdmin\Models;
use Modules\AuthAdmin\Services\QueryService;
use Modules\AuthAdmin\Services\CommonService;
class ShopPicture extends BaseApiModel
{
	/**
	 * @name  关联商品图片   一对一
	 */
	 public function image_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','image_id');
    }
	/**
	 * @name 是否跳转
	 */
	private function the_jump_list(){
		return [
			'0'=>'否',
			'1'=>'跳转网页',
			'2'=>'跳转小程序页面',
			'3'=>'跳转详情'
		];
	}
	/**
	 * @name 图片类型
	 */
	private function type_list(){
		return [
			'1'=>'首页轮播图',
			'2'=>'咨询轮播图',
			'3'=>'首页直播广告图'
		];
	}
	/**
     * @name   是否跳转:0=否,1=跳转网页,2=跳转小程序页面,3=跳转详情
     * @param  int  $value
     * @return string 
     */
    public function getTheJumpAttribute($value)
    {
        return $this->the_jump_list()[$value];
    }
	/**
     * @name   图片类型:1=首页轮播图,2=咨询轮播图,3=首页广告图	
     * @param  int  $value
     * @return string 
     */
    public function getTypeAttribute($value)
    {
        return $this->type_list()[$value];
    }
	/**
     * @name   状态0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getStatusAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
     * @name   是否往期0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getOpenAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
     * @name   更新时间为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value?$value:'';
    }
	/**
	 * @name  列表数据
	 * @param  data  array 搜索数据
	 * @return json 
	 */
    public function list_data($data){
		$model = $this->query();
		$model = (new QueryService())->query_condition($model,$data);
		if(isset($data['the_jump']) && $data['the_jump']>=0){
			$model = $model->where('the_jump',$data['the_jump']);
		}
		if(isset($data['type']) && $data['type']>0){
			$model = $model->where('type',$data['type']);
		}	
        $list = $model->select('id','url','image_id','the_jump','status','created_at','updated_at','sort','type','open')
				->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->paginate($data['pagesize'])
				->toArray(); 
		$list['data'] = $this->common_image_one($list['data']);		
		return $this->api_success(['data'=>[
			'list'=>$list['data'],
			'total'=>$list['total']
		]]);		
	}
	/**
	 * @name  编辑页面
	 * @param  id int 轮播图ID
	 * @return  JSON
	 */
	public function get_edit($id){
		$data = $this->select('id','url','image_id','the_jump','sort','content','type','title')
					->with([
						'image_one'=>function($query){
							$query->select('id','url','open');  
						}
					])->find($id);		
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		if(isset($data['image_one']) && $data['image_one']){
			if($data['image_one']['open'] == 1){
				$data['image_url'] = $http.$data['image_one']['url'];
			}else{
				$data['image_url'] = $data['image_one']['url'];
			}
		}
		$data['the_jump_value'] = $data->getOriginal('the_jump');
		$data['type_value'] = $data->getOriginal('type');
		unset($data['image_one']);
		$data['content'] = $CommonService->replace_pic_url($data['content']);
		return $this->api_success([
			'data'=>$data
		]);
	}
}
